package com.neusoft.yiyang.care.controller;

import com.neusoft.yiyang.care.service.CareRecordModifyRequestService;
import com.neusoft.yiyang.common.entity.care.CareRecordModifyRequest;
import com.neusoft.yiyang.common.enums.ApprovalStatus;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.Date;
import java.util.List;

@Tag(name = "护理记录修改申请接口", description = "管理护理记录修改的申请、审批流程")
@RestController
@RequestMapping("/care-record-modify-requests")
public class CareRecordModifyRequestController {

    @Autowired
    private CareRecordModifyRequestService modifyRequestService;

    @Operation(summary = "提交修改申请", description = "护工提交对护理记录的修改申请（需管理员审批）")
    @PostMapping
    public void submitModifyRequest(
            @Parameter(description = "申请人ID（护工ID）", required = true) @RequestParam Integer applicantId,
            @Parameter(description = "原护理记录ID", required = true) @RequestParam Integer originalRecordId,
            @Parameter(description = "修改后的护理项目ID（可选）") @RequestParam(required = false) Integer careItemId,
            @Parameter(description = "修改后的护工ID（可选）") @RequestParam(required = false) Integer caretakerId,
            @Parameter(description = "修改后的护理时间（可选，格式：yyyy-MM-dd HH:mm:ss）")
            @RequestParam(required = false) @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") Date careTime,
            @Parameter(description = "修改后的执行次数（可选）") @RequestParam(required = false) Integer quantity,
            @Parameter(description = "修改后的反馈内容（可选）") @RequestParam(required = false) String feedback,
            @Parameter(description = "修改后的状态（可选）") @RequestParam(required = false) String status,
            @Parameter(description = "修改证明照片（可选，支持PNG/JPG格式，不超过5MB）")
            @RequestParam(required = false) MultipartFile file) {
        modifyRequestService.submitModifyRequest(
                applicantId, originalRecordId, careItemId, caretakerId,
                careTime, quantity, feedback, status, file
        );
    }

    @Operation(summary = "审批修改申请", description = "管理员对护理记录修改申请进行审批（通过/拒绝）")
    @PutMapping("/{requestId}/approve")
    public void approveModifyRequest(
            @Parameter(description = "申请ID", required = true) @PathVariable Integer requestId,
            @Parameter(description = "审批状态（PENDING=待审批，APPROVED=通过，REJECTED=拒绝）", required = true)
            @RequestParam ApprovalStatus status,
            @Parameter(description = "审批人ID（管理员ID）", required = true) @RequestParam Integer approverId,
            @Parameter(description = "审批备注（可选）") @RequestParam(required = false) String approvalRemark) {
        modifyRequestService.approveModifyRequest(requestId, status, approverId, approvalRemark);
    }

    @Operation(summary = "查询修改申请列表", description = "管理员查看所有护理记录修改申请，支持按原记录ID和审批状态筛选")
    @GetMapping
    public List<CareRecordModifyRequest> getAllRequests(
            @Parameter(description = "原护理记录ID（可选）") @RequestParam(required = false) Integer originalRecordId,
            @Parameter(description = "审批状态（可选，PENDING/APPROVED/REJECTED）")
            @RequestParam(required = false) String approvalStatus) {
        return modifyRequestService.getAllRequests(originalRecordId, approvalStatus);
    }

    @Operation(summary = "更新修改申请", description = "护工更新已提交的修改申请（如被驳回后重新提交）")
    @PutMapping("/{requestId}")
    public void updateModifyRequest(
            @Parameter(description = "申请ID", required = true) @PathVariable Integer requestId,
            @Parameter(description = "申请人ID（护工ID）", required = true) @RequestParam Integer applicantId,
            @Parameter(description = "原护理记录ID", required = true) @RequestParam Integer originalRecordId,
            @Parameter(description = "修改后的护理项目ID（可选）") @RequestParam(required = false) Integer careItemId,
            @Parameter(description = "修改后的护工ID（可选）") @RequestParam(required = false) Integer caretakerId,
            @Parameter(description = "修改后的护理时间（可选，格式：yyyy-MM-dd HH:mm:ss）")
            @RequestParam(required = false) @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") Date careTime,
            @Parameter(description = "修改后的执行次数（可选）") @RequestParam(required = false) Integer quantity,
            @Parameter(description = "修改后的反馈内容（可选）") @RequestParam(required = false) String feedback,
            @Parameter(description = "修改后的状态（可选）") @RequestParam(required = false) String status,
            @Parameter(description = "修改证明照片（可选，支持PNG/JPG格式，不超过5MB）")
            @RequestParam(required = false) MultipartFile file) {

        modifyRequestService.updateModifyRequest(
                requestId, applicantId, originalRecordId, careItemId, caretakerId,
                careTime, quantity, feedback, status, file
        );
    }
}